package LC;

import javafx.util.Pair;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @ClassName LC56
 * @Description: TODO
 * @Author hl
 * @Date 2020/9/8
 * @Version V1.0
 **/
public class LC56 {

    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0]-o2[0];
            }
        });
        int index=-1;
        int [][]res=new int[intervals.length][2];
        for(int i=0;i<intervals.length;i++){
            if(index==-1||res[index][1]<intervals[i][0]){
                index++;
                res[index]=intervals[i];
            }else{
                index++;
                res[index][0]=Math.min(res[index][0],intervals[i][0]);
                res[index][1]=Math.max(res[index][1],intervals[i][1]);

            }

        }
        return Arrays.copyOf(res,index);
    }
}
